package com.example.perplonews.a_d_customview

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView

class Bottom_VIew : LinearLayout, View.OnClickListener {
    private var params: LinearLayout.LayoutParams? = null

    //图片和文字
    private var imageView: ImageView? = null
    private var textView: TextView? = null

    //接口
    private var clickbar: ClickBar? = null


    constructor(context: Context, attr: AttributeSet) : super(context, attr) {
        init()
    }

    constructor(context: Context) : super(context) {
        init()
    }

    private fun init() {
        orientation = VERTICAL
        gravity = Gravity.CENTER
        params = LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0)
        params?.weight = 1f
    }

    //添加图片
    fun addselectimage(imgid: Int): Bottom_VIew {
        imageView = ImageView(context)
        imageView?.layoutParams = params
        imageView?.setImageResource(imgid)
        imageView?.setPadding(5, 5, 5, 5)

        return this
    }

    //添加文字
    fun addtext(text: String, unselect: Int): Bottom_VIew {
        textView = TextView(context)
        textView?.setText(text)
        textView?.gravity = Gravity.CENTER
        textView?.textSize = 13f
        textView?.layoutParams = params
        textView?.setPadding(10, 10, 10, 10)
        textView?.setTextColor(unselect)

        return this
    }

    //展示图片和文字
    fun show() {
        addView(imageView)
        addView(textView)
        setOnClickListener(this::onClick)
    }

    //选中接口回调
    interface ClickBar {
        fun onItem(id: Int)
    }

    //初始化接口
    fun setListenner(clickBar: ClickBar): Bottom_VIew {
        this.clickbar = clickBar
        return this
    }


    override fun onClick(v: View?) {
        if (clickbar != null) {
            clickbar?.onItem(id)
        }
    }

    //选中的话调用此方法
    fun selectbar(id: Int, color: Int) {
        imageView?.setImageResource(id)
        textView?.setTextColor(color)
    }

    fun unselectbar(id: Int, color: Int) {
        imageView?.setImageResource(id)
        textView?.setTextColor(color)
    }
}